package com.ymy.cckd.web;

import com.ymy.cckd.model.EbProduct;
import com.ymy.cckd.model.EbProductCategory;
import com.ymy.cckd.service.EbProductCategoryService;
import com.ymy.cckd.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller//迎宾证
@RequestMapping("/product")
public class EbProductController {


    @Autowired
    private EbProductService ebProductService;

    @Autowired
    private EbProductCategoryService ebProductCategoryServcie;

    @RequestMapping("/detail")
    public String detail(Model model, Integer epId) {
        System.out.println("=====浏览器点击了某个商品======:epId==" + epId);
        //1.调用service查询该商品的详细信息
        EbProduct productDetail = ebProductService.detail(epId);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryServcie.selectBigAndSmallCate();
        //2.把数据绑定到model
        model.addAttribute("detail", productDetail);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap", categoryMap);
        //3.返回视图名称
        return "product";//templates/product-view.html
    }


    //product/cate_product_list/123
    @RequestMapping("/cate_product_list/{cateId}")
    public String productList(Model model,
                              @PathVariable("cateId") Integer cateId,
                              @RequestParam(value = "pSize", defaultValue = "1") Integer pSize,
                              @RequestParam(value = "pageSize", defaultValue = "12") Integer pageSize) {
        System.out.println("=======进入productList 这个方法=====cateId:" + cateId);
        //1.查询所有属于该二级分类的商品
        List<EbProduct> products = ebProductService.selectByCateId(cateId, pSize, pageSize);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryServcie.selectBigAndSmallCate();
        //2.将所属分类商品数据绑定到model中

        model.addAttribute("products", products);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap", categoryMap);
        //返回视图
        return "category";
    }

   @RequestMapping("/search")
    public String search(Model model, String searchText) {
        System.out.println("==search== searchText=" + searchText);
        //1.调用service层的按searchText进行模糊查询
        List<EbProduct> productList = ebProductService.selectByCondition(searchText);
        //2.查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap =ebProductCategoryServcie.selectBigAndSmallCate();

        //3.将查询数来的数据绑定到model中
        model.addAttribute("products", productList);

        //4.传递查询条件
        model.addAttribute("searchText", searchText);

        //5.将商品分类进行绑定
        model.addAttribute("categoryMap", categoryMap);
        //6.返回视图：product-list.jsp
        return "category";

    }
    /**
     * ajax发送请求（推荐）
     * @param epId
     */
   @RequestMapping("/add_car")
    public String addCar(HttpSession session, int epId){
        System.out.println("addCar===epId=="+epId);
        //1.调用service根据id查询商品的方法
        EbProduct target_product = ebProductService.detail(epId);
        //2.获取购物车  session
        //3 获取集合 商品---集合--购物车
        List<EbProduct> car = (List<EbProduct>) session.getAttribute("car");
        if(car==null){
            car= new ArrayList<>(10);
        }
        //4.将商品放入集合
        //4.1 判断集合中是否已经存在该商品
        boolean flag = false;//假设购物车不存在该商品
        for (EbProduct originProduct:car){
            //下面条件成立 表名购物车中存在该商品，只需要修改商品的数量就可以 ，
            //不需要再将该商品添加到购物车
            if(target_product.getEpId().equals(originProduct.getEpId())){
                flag= true;
                originProduct.setCount(originProduct.getCount()+1);
                break;
            }
        }
        if(!flag){
            car.add(target_product);
        }
        //5.将集合放入购物车 key-value
        session.setAttribute("car",car);
        return "redirect:/product/detail?epId="+epId;
    }
    @RequestMapping("/show_car")
    public String showCar(){

        return "shopping";//shopping.jsp
    }

    @RequestMapping("/shoping_result")
    public String showResult(HttpSession session){
        //1.清除购物车
        session.removeAttribute("car");
        return "shopping-result";
    }


}
